package test;

import algorithm.leetcode.TopKFrequent;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @auther zhouliangliang
 * @date 2022/10/10 23:16
 * @description
 */
public class TopK {

    private int topk(int[] array, int k) {
        PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 > o2 ? 1 : -1;
            }
        });

        for(int i=0; i<array.length; i++) {
            queue.add(array[i]);
            if(queue.size() > k) {
                queue.poll();
            }
        }
        return queue.peek();
    }

    public static void main(String[] args) {
      int value =  new TopK().topk(new int[]{1,4,2,5,6}, 2);
        System.out.println(value);
    }

}
